package cn.szzsi._admin;

import cn.szzsi.common.model.Activity;
import cn.szzsi.common.model.Member;
import cn.szzsi.common.model.SmsRecord;
import cn.szzsi.common.sms.SmsZhuanxinUtil;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;


public class SmsController extends BaseAdminController<Activity> {
    private String active_status;
    private String phone;
    private String username;

    // 列表
    public void list() {
        render("product_sms.html");
    }
    //获取短信发送记录
    public void getListData() {
        List<SmsRecord> ls = SmsRecord.me.find("select * from mall_sms_record order by createt_time desc ");
        renderJson(ls);
    }

    public void getListUserData() {
        String shopId=getAttr("shopId");
        List<Member> ls = Member.dao.find("select * from  mall_member  where  shopId =? and  phone is not null ",shopId);
        renderJson(ls);
    }


    //删除短信发送记录
    // 删除
    public void delete() {
        ids = getParaValues("ids");
        if (ids != null && ids.length > 0) {
            for (String id : ids) {
//                SmsRecord smsRecord = SmsRecord.me.findById(id);
                if (SmsRecord.me.deleteById(id)) {
                    ajaxJsonSuccessMessage("删除成功！");
                } else {
                    ajaxJsonErrorMessage("删除失败！");
                }
            }
            }else {
            ajaxJsonErrorMessage("未选中，删除失败！");
        }




    }
    // 跳转到短信群发操作界面
    public void addclerk() {

        render("SendSmsAdd.html");
    }

    // 跳转到发送短信联系人界面
    public void poiPage() {

        render("Sms_poiPage.html");
    }

    //删除短信发送记录 单行
    public void activeStatusUpdate() {
         active_status=getAttr("active_status");
         render("Sms_poiPage.html");
    }

    //群发短信
    public void SendSms() {
        String sign=getPara("staff.content");//签名
        String content=getPara("staff.account");//内容
        int num = Integer.valueOf(getPara("numOfSend"));//数量
        String usernames=getPara("usernames");
        String phones=getPara("phones");
        //去除phone最后面的逗号
        if (phones.length() > 0) {
            phone =phones.substring(0, phones.length()-1);
        }
        //去除usernames最后面的逗号
        if (usernames.length() > 0) {
            username =usernames.substring(0, usernames.length()-1);
        }

        SmsZhuanxinUtil.sendToSms(phone,sign,content);
        UpdateToSql(num,sign,content,phone,username);
        renderJson(Ret.ok("info","成功!").set("url","list"));
    }

    private void UpdateToSql(int num, String title, String content, String phone, String username) {
        String[]  phones=phone.split(",");
        String[]  usernames=username.split(",");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String createt_time =df.format(new Date());// new Date()为获取当前系统时间

        for (int i = 0; i <num ; i++) {
            String id = UUID.randomUUID().toString().replaceAll("-", "");
         String phone1 = phones[i].toString();
         String username1 = usernames[i].toString();
            // INSERT INTO `mall_sms_record` (`id`, `name`, `phone`, `content`, `createt_time`) VALUES ('3', '啊啊', '33', '啊啊', '订单')

            Record record = new Record().set("id", id).set("status", "1").set("name", username1).set("phone", phone1).set("content", content).set("createt_time", createt_time);
            Db.save("mall_sms_record", record);

        }

    }





}




